using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobCreateInDocs.Features.JobCreateInDocs
{
    /// <summary>
    /// Этот класс обрабатывает события, возникающие в ходе активации, деактивации, установки, удаления и обновления компонентов.
    /// </summary>
    /// <remarks>
    /// GUID, присоединенный к этому классу, может использоваться при создании пакета и не должен изменяться.
    /// </remarks>
    [Guid("3396c074-fd5a-4057-9aac-04f59845035d")]
    public class JobCreateInDocsEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                // delete preview job item
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (
                           job.Name == "JobCreateInDocs_v2"
                        || job.Name == "JobCreateInDocs_v8"
                        )
                    {
                        job.Delete();
                    }
                }
                JobCreateInDocs listLoggerJob =
                    new JobCreateInDocs("JobCreateInDocs_v8", site.WebApplication);
                SPMinuteSchedule schedule = new SPMinuteSchedule();
                schedule.BeginSecond = 0;
                schedule.EndSecond = 59;
                schedule.Interval = 25;
                //SPSchedule schedule = SPSchedule.FromString("every 20 minutes between 7:00:00 and 23:00:00");
                listLoggerJob.Schedule = schedule;
                listLoggerJob.Update();
            }
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (job.Name == "JobCreateInDocs_v2"
                        || job.Name == "JobCreateInDocs_v8")
                    {
                        job.Delete();
                    }
                }
            }
        }
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
